package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.StockMapper;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.StockService;
import com.atguigu.jxc.util.DateUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;
    @Override
    public void saveDamageListGoods(DamageList damageList, List<DamageListGoods> damageListGoodsStr) {
        try {
            stockMapper.saveDamageList(damageList);
            DamageList damage = stockMapper.getDamageListByDamageNumber(damageList.getDamageNumber());
            if (null!=damage){
            for (DamageListGoods damageListGoods : damageListGoodsStr) {
                damageListGoods.setDamageListId(damage.getDamageListId());
            }
            stockMapper.saveDamageListGoodsStr(damageListGoodsStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void saveOverflowListGoods(OverflowList overflowList, List<OverflowListGoods> list) {
        try {
            stockMapper.saveOverflowList(overflowList);
            OverflowList overflow = stockMapper.getOverflowListByOverflowNumber(overflowList.getOverflowNumber());
            if (null!=overflow){
                for (OverflowListGoods overflowListGoods : list) {
                    overflowListGoods.setOverflowListId(overflow.getOverflowListId());
                }
                stockMapper.saveOverflowListGoods(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> listAlarm() {
        List<Goods> list = stockMapper.listAlarm();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @SneakyThrows
    @Override
    public Map<String, Object> damageListGoodsList(String sTime, String eTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = dateFormat.parse(sTime);
        Date end = dateFormat.parse(eTime);
        List<DamageList>list = stockMapper.damageListGoodsList(sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> damageGoodsList(Integer damageListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<DamageListGoods> list = stockMapper.damageGoodsList(damageListId);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> overflowList(String sTime, String eTime) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date start = dateFormat.parse(sTime);
//        Date end = dateFormat.parse(eTime);
        List<OverflowList>list = stockMapper.overflowList(sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> overflowListGoodsList(Integer overflowListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowListGoods> list = stockMapper.overflowListGoodsList(overflowListId);
        map.put("rows",list);
        return map;
    }
}
